<?php
class M_news_and_events extends CI_Model {
	
	private $__table = 'announcements';
	
	/*
		query to count all grade level section for pagnation
		11/12/11/2012
	*/	
	public function count_news_and_events()
	{
		return $this->db->count_all($this->__table);
	}
	
	/*
		query to get a/all record
		11/12/11/2012
	*/	
	public function get_news_and_events($id=FALSE)
	{
		if($id == FALSE){
			$query = $this->db->get($this->__table);
		}else{
			$query = $this->dbwhere('id',$id)->get($this->__table);
		}
		
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	/*
		query the record for the day
	*/	
	public function get_news_and_events_today($year,$month,$day)
	{
		$y = $year;
		$m = $month;
		$d = $day;
		$dater = $y.'-'.$m.'-'.$d;
		$sql = "SELECT *  FROM announcements WHERE DATE(announcement_date) = '$dater'";
		$query = $this->db->query($sql); 
		
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	/*
		@param array
		adds news and events to database
		12/11/2012
	*/
	public function add_news_and_events($input)
	{
		if($this->verify_data($input,2) == FALSE)
		{
			$input['created_at'] = NOW;
			$this->db->insert($this->__table,$input);
			return $this->db->affected_rows() > 0 ? array('status'=>'true') : array('status'=>'false');
		}else
		{
			return array('status'=>'exist');
		}
	}
	
	/* verify_data
	 * @param array
	 * @param int
	*  verifies if data entered by user is existing already in the database
	*  $strict level 1,2
	*  level 1 = if one field has existing data in database returns true else false
	*  level 2 = if all field has existing data in database returns true else false
	*  12/11/2012
	*/
	public function verify_data($data,$strict_level)
	{
			if($strict_level == 1)
			{
				$query = $this->db->or_where($data)->get($this->__table);
			}elseif($strict_level == 2){
				$query = $this->db->where($data)->get($this->__table);
			}
			return $query->num_rows() > 0 ? TRUE : FALSE;
	}
}
?>